﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dukou.Search
{
    /// <summary>
    /// 
    /// </summary>
    public class SearchResult : ISearchResult
    {
        private IList<ISearchResultItem> m_items;

        private int m_totalCount;

        public SearchResult(ISearchContext context)
            : this(context, 0)
        {
        }

        public SearchResult(ISearchContext context, int totalCount)
        {
            this.SearchContext = context;
            this.TotalCount = totalCount;
            this.m_items = new List<ISearchResultItem>();
        }

        public ISearchContext SearchContext { get; private set; }

        public int TotalCount 
        {
            get
            {
                if (this.m_totalCount == 0)
                {
                    return this.m_items.Count;
                }

                return this.m_totalCount;
            }
            private set
            {
                this.m_totalCount = value;
            }
        }

        /// <summary>
        /// 获取 当前页的结果条目集合
        /// </summary>
        public IEnumerable<ISearchResultItem> Items 
        { 
            get 
            { 
                return this.m_items; 
            } 
        }

        /// <summary>
        /// 获取 当前页的底层结构条目集合
        /// </summary>
        public IEnumerable<object> UnderlayerItems
        {
            get
            {
                return this.m_items.Select(x => x.UnderlayerObject);
            }
        }

        /// <summary>
        /// 添加当前页的结果条目
        /// </summary>
        /// <param name="item"></param>
        public void AddItem(ISearchResultItem item)
        {
            this.m_items.Add(item);
        }
    }
}
